package com.minhua.jijiehao2.mainframe.home.menulistactivity.impl

import android.content.Context
import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import android.widget.TextView
import com.bumptech.glide.Glide
import com.minhua.jijiehao2.R
import com.minhua.jijiehao2.base.BaseActivity
import com.minhua.jijiehao2.base.adapter.ViewHolder
import com.minhua.jijiehao2.base.adapter.recyclerview.BaseCommonAdapter
import com.minhua.jijiehao2.bean.ChannelBean
import com.minhua.jijiehao2.constants.DefineKey
import com.minhua.jijiehao2.mainframe.common.webviewactivity.impl.WebActivity
import com.minhua.jijiehao2.mainframe.home.menulistactivity.imp.IMenuListView
import com.minhua.jijiehao2.mainframe.launcher.loginactivity.impl.LoginActivity
import kotlinx.android.synthetic.main.activity_menu_list.*

class MenuListActivity : BaseActivity<IMenuListView, MenuListPresenter>(), IMenuListView {

    override fun getLayoutId(): Int = R.layout.activity_menu_list

    override fun getPresenter(): MenuListPresenter = MenuListPresenter()

    companion object {
        fun intentStart(context: Context, type: Int, title: String) {
            val intent = Intent(context, MenuListActivity::class.java)
            intent.putExtra(DefineKey.CHANNEL_TYPE, type)
            intent.putExtra("title", title)
            context.startActivity(intent)
        }
    }

    private val listData = arrayListOf<ChannelBean>()
    private var adapter: BaseCommonAdapter<ChannelBean>? = null

    override fun initView() {
        ctTitle.setTitle(intent.getStringExtra("title"))
        adapter = object : BaseCommonAdapter<ChannelBean>(this, R.layout.item_channel_list, listData) {
            override fun convert(holder: ViewHolder?, t: ChannelBean?) {
                holder?.setText(R.id.tvName, t?.pname ?: "")
                holder?.setText(R.id.tvMessage, t?.pmessage ?: "")
                val split = t?.ptag?.split("|")
                if (split != null) {
                    if (split.size == 1) {
                        holder?.setText(R.id.tvTagLeft, split[0])
                        holder?.setVisible(R.id.tvTagCenter, View.INVISIBLE)
                    } else {
                        holder?.setText(R.id.tvTagLeft, split[0])
                        holder?.setVisible(R.id.tvTagCenter, View.VISIBLE)
                        holder?.setText(R.id.tvTagCenter, split[1])
                    }
                }
                val type = intent.getIntExtra(DefineKey.CHANNEL_TYPE, 2)
                if (type == 32) {
                    holder?.getView<TextView>(R.id.tvDate)?.visibility = View.INVISIBLE
                } else {
                    holder?.getView<TextView>(R.id.tvDate)?.visibility = View.VISIBLE
                    holder?.setText(R.id.tvDate, "日利率:" + t?.pinterest + "%")
                }
                holder?.setText(R.id.tvNum, "申请人数:" + t?.pBorrowNum)
                holder?.setText(R.id.tvTag, t?.pmarks ?: "")

                holder?.setVisible(R.id.rlTab, View.GONE)

                Glide.with(this@MenuListActivity).load(t?.picUrl).into(holder?.getView(R.id.ivChannelIcon))
                holder?.setOnClickListener(R.id.rlItemView, {
                    //                    ChannelDetailActivity.intentStart(this@MenuListActivity, t)
                    if (LoginActivity.logged(this@MenuListActivity)) {
                        WebActivity.intentStart(this@MenuListActivity, t?.phttp!!, t.pname)
                    }
                })
            }
        }
        rvContent.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
        rvContent.adapter = adapter

        refresh.setOnRefreshListener { mPresenter.getListChannel() }
    }

    override fun initData() {
        mPresenter.getListChannel()
    }

    override fun getChannelType(): Int = intent.getIntExtra(DefineKey.CHANNEL_TYPE, 2)

    override fun onChannelListSuccess(list: List<ChannelBean>) {
        refresh.isRefreshing = false
        if (list.isNotEmpty()) {
            listData.clear()
            listData.addAll(list)
            adapter?.notifyDataSetChanged()
        }
    }
}
